package com.example.suduku;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {

    protected Button btnEasy,btnPutong,btnHard;
    private ImageButton toggleButton;
    private boolean isPlaying = false;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        Intent musicIntent = new Intent(this, BackgroundMusicService.class);
        startService(musicIntent);

        btnEasy = findViewById( R.id.btn_easy );
        btnPutong = findViewById( R.id.btn_putong );
        btnHard = findViewById( R.id.btn_hard );
        toggleButton = findViewById( R.id.toggleButton );


        toggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent toggleIntent = new Intent(MainActivity.this, BackgroundMusicService.class);
                toggleIntent.setAction("ACTION_TOGGLE");
                startService(toggleIntent);
                isPlaying = !isPlaying;
                updateButtonImage();
            }
        });
        // 设置按钮的点击监听事件
        btnEasy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 Intent，跳转到 SuduActivity 页面
                Intent intent = new Intent(MainActivity.this, SuduActivity.class);

                // 获取按钮的 ID
                int buttonId = btnEasy.getId();

                // 将按钮的 ID 作为 extra 传递
                intent.putExtra("button_id", buttonId);

                // 启动 SuduActivity 页面
                startActivity(intent);
            }
        });

        btnPutong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 Intent，跳转到 SuduActivity 页面
                Intent intent = new Intent(MainActivity.this, SuduActivity.class);

                // 获取按钮的 ID
                int buttonId = btnPutong.getId();

                // 将按钮的 ID 作为 extra 传递
                intent.putExtra("button_id", buttonId);

                // 启动 SuduActivity 页面
                startActivity(intent);
            }
        });

        btnHard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 Intent，跳转到 SuduActivity 页面
                Intent intent = new Intent(MainActivity.this, SuduActivity.class);

                // 获取按钮的 ID
                int buttonId = btnHard.getId();

                // 将按钮的 ID 作为 extra 传递
                intent.putExtra("button_id", buttonId);

                // 启动 SuduActivity 页面
                startActivity(intent);
            }
        });

    }
    protected void onDestroy() {
        super.onDestroy();
        // 停止背景音乐服务
        Intent musicIntent = new Intent(this, BackgroundMusicService.class);
        musicIntent.setAction("ACTION_STOP");
        stopService(musicIntent);
    }
    private void updateButtonImage() {
        if (isPlaying) {
            toggleButton.setImageResource(R.drawable.baseline_music_off_24);
        } else {
            toggleButton.setImageResource(R.drawable.baseline_music_note_24);
        }
    }
}
